import java.util.concurrent.FutureTask;
import java.util.concurrent.Semaphore;

public class ThreadDemo6 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(4);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("申请资源成功");
                    semaphore.acquire();
                    System.out.println("获取到了资源");
                    Thread.sleep(1000);
                    semaphore.release();
                    System.out.println("释放了资源");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(runnable);
            t.start();
        }
    }
}
